package com.yelobblog.yelobblog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yelobblog.yelobblog.domain.Vo.articlesVo;
import com.yelobblog.yelobblog.domain.entity.articles;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
//import org.apache.ibatis.annotations.Param;

@Mapper
public interface articlesMapper extends BaseMapper<articles> {

    @Insert("insert into articles (title,description,content, categoryId, authorId) values (#{title},#{description},#{content}, #{categoryId}, #{authorId})")
    void insert(String title, String description,String content, Integer categoryId, Integer authorId);

    articlesVo selectById(Integer id);

    Page<articlesVo> selectList(Page<?> page,Integer status);

    Page<articlesVo> selectListByCategoryId(Page<?> page, Integer categoryId,Integer status);

    Page<articlesVo> selectListByAuthorId(Page<?> page, Integer authorId,Integer status);

    @Select("SELECT id,title FROM articles where status=1 order by updateTime desc limit 7")
    List<articlesVo> selectListByUpdateTime();

    @Select("SELECT id,title FROM articles where status=1 order by viewCount desc limit 7")
    List<articlesVo> selectListByViewCount();

    Integer likeArticle(Integer id);

    Integer dislikeArticle(Integer id);

    @Update("update articles set status = #{status} where id = #{id}")
    void updateStatus(Integer id, Integer status);

    @Update("update articles set viewCount=viewCount+1 where id = #{articleId}")
    void addViewCount(Integer articleId);

    @Select("SELECT id,title FROM articles where status=1 order by viewCount desc limit 5")
    Page<articlesVo> selectListByStatus(Page<?> page);

    @Select("SELECT * FROM articles WHERE (title LIKE CONCAT('%', #{keyword}, '%') OR content LIKE CONCAT('%', #{keyword}, '%')) AND status = #{status}")
    Page<articlesVo> searchArticles(Page<?> page, String keyword, Integer status);
}

